package configuration

import (
	"encoding/json"
	"kop/modules/configuration/client"
	"kop/util/log"
	"sort"
)

func init() {

	var m = make(map[int]VIPConf)

	if err := json.Unmarshal(client.Load("VIPTemplate"), &m); err != nil {
		panic(err)
	}

	for _, v := range m {
		VIPConfigs = append(VIPConfigs, v)
	}
	sort.Sort(vips(VIPConfigs))

	log.Info("Load VIPTemplate.json success.")

}

type (
	VIPConf struct {
		VIP         int32 // VIP等级
		Diamond     int32 // VIP升级需求点数
		AwardID     int32 // VIP等级奖励
		Drink       int32 // 酒吧请酒体力上限
		Date        int32 // 酒吧约会精力上限
		Wish        int32 // 艳遇改运免费次数
		Shop        int32 // 酒吧商店刷新上限
		Benediction int32 // 女神赐福每日次数
	}

	vips []VIPConf
)

var VIPConfigs []VIPConf

func (c vips) Len() int           { return len(c) }
func (c vips) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c vips) Less(i, j int) bool { return c[i].VIP < c[j].VIP }

func GetVIPByDiamond(diamond int32) int32 {

	for _, conf := range VIPConfigs {
		if diamond < conf.Diamond {
			return conf.VIP
		}
	}

	return VIPConfigs[len(VIPConfigs)-1].VIP
}

func GetByVIP(vip int32) VIPConf {

	for _, conf := range VIPConfigs {
		if vip == conf.VIP {
			return conf
		}
	}

	return VIPConfigs[len(VIPConfigs)-1]
}
